<?php
/**
 * Created by IntelliJ IDEA.
 * User: XD
 * Date: 2017/8/23
 * Time: 16:36
 */
 $var1 = 1;
 $var2 = 2;

 function test(){
     $GLOBALS['var2'] = &$GLOBALS['var1'];
 }
 test();
 echo $var2;  // 1 说明 var2 指向了var1的空间

 $var3 = 1;
 $var4 = 2;
 function test2(){
     global $var3,$var4;
     $var4 = &$var3;
     echo $var4;  //1
     $var4 = "snsgou.com";
 }

 test2();
 echo $var4;  //2
 echo $var3;  //snsgou.com
// 理解  局部变量只是指向全局变量而已  只是一个引用

$var5 = 1;
function test3(){
    unset($GLOBALS['var5']);  // 被删除  不会打印任何东西
}
echo "<br>";
test3();
echo $var5;

$var6 = 8;
function test4(){
    global  $var6;
    unset($var6);  // 只是断开了内存地址和变量的链接
}
echo "<br>";
echo $var6;  //8

echo "+++++++++++++++++++++++++++++++++";
echo "<br>";
$var7 = "Example variable";
$var8 = "";

function global_references($use_globals) {
    global $var7, $var8;
    if (!$use_globals) {
        $var8 = &$var7; // visible only inside the function
    } else {
        $GLOBALS["var8"] = &$var7; // visible also in global context
    }
}

global_references(false);
echo "var2 is set to '$var8'"; // var2 is set to ''
echo "<br>";
global_references(true);
echo "var2 is set to '$var8'"; // var2 is set to 'Example variable'

//这将使 foo 函数中的 $var 变量在函数调用时和 $bar 绑定在一起，但接着又被重新绑定到了 $GLOBALS["baz"] 上面。
$bar = 3;
function foo(&$var){
    $GLOBALS["baz"] = 5;
    $var = &$GLOBALS["baz"];
}
foo($bar);
echo $bar;//输出3

$a = 1;
$b = 2;
function Sum() {
    global $a, $b;
    $b = $a + $b;
}
Sum();
echo $b; //3
?>